﻿using Microsoft.EntityFrameworkCore;
using Wms.Core.Entities;

namespace Wms.Data;

public class WarehouseDbContext : DbContext
{
	public WarehouseDbContext(DbContextOptions<WarehouseDbContext> options) : base(options) { }

	public DbSet<Item> Items { get; set; }
	public DbSet<Location> Locations { get; set; }
	public DbSet<Warehouse> Warehouses { get; set; }
	public DbSet<Inventory> Inventories { get; set; }
	public DbSet<StockInOrder> StockInOrders { get; set; }
	public DbSet<StockInDetail> StockInDetails { get; set; }

	public DbSet<StockOutOrder> StockOutOrders { get; set; }
	public DbSet<StockOutDetail> StockOutDetails { get; set; }

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		// 入库单明细
		modelBuilder.Entity<StockInDetail>()
			.HasOne(d => d.StockInOrder)
			.WithMany(o => o.Details)
			.HasForeignKey(d => d.StockInOrderId);

		modelBuilder.Entity<StockInDetail>()
			.HasOne(d => d.Item)
			.WithMany()
			.HasForeignKey(d => d.ItemId);

		// 出库单明细
		modelBuilder.Entity<StockOutDetail>()
			.HasOne(d => d.StockOutOrder)
			.WithMany(o => o.Details)
			.HasForeignKey(d => d.StockOutOrderId);

		modelBuilder.Entity<StockOutDetail>()
			.HasOne(d => d.Item)
			.WithMany()
			.HasForeignKey(d => d.ItemId);

		// 库存
		modelBuilder.Entity<Inventory>()
			.HasOne(i => i.Item)
			.WithMany()
			.HasForeignKey(i => i.ItemId);

		modelBuilder.Entity<Inventory>()
			.HasOne(i => i.Location)
			.WithMany()
			.HasForeignKey(i => i.LocationId);
	}
}
